package com.sponsorpay.sdk.android.publisher;

import com.sponsorpay.sdk.android.utils.SignatureTools;
import com.sponsorpay.sdk.android.utils.SponsorPayLogger;
import org.json.JSONObject;

public abstract class AbstractResponse
{
  protected static final String ERROR_CODE_KEY = "code";
  protected static final String ERROR_MESSAGE_KEY = "message";
  protected String mErrorCode;
  protected String mErrorMessage;
  protected RequestErrorType mErrorType;
  protected String mResponseBody;
  protected String mResponseSignature;
  protected int mResponseStatusCode;

  public String getErrorCode()
  {
    if (this.mErrorCode != null)
      return this.mErrorCode;
    return "";
  }

  public String getErrorMessage()
  {
    if (this.mErrorMessage != null)
      return this.mErrorMessage;
    return "";
  }

  public RequestErrorType getErrorType()
  {
    return this.mErrorType;
  }

  public boolean hasErrorStatusCode()
  {
    return (this.mResponseStatusCode < 200) || (this.mResponseStatusCode > 299);
  }

  public abstract void onErrorTriggered();

  public abstract void onSuccessfulResponseParsed();

  public void parseAndCallListener(String paramString)
  {
    if (this.mErrorType == RequestErrorType.ERROR_NO_INTERNET_CONNECTION)
    {
      onErrorTriggered();
      return;
    }
    if (hasErrorStatusCode())
    {
      parseErrorResponse();
      onErrorTriggered();
      return;
    }
    if (!verifySignature(paramString))
    {
      onErrorTriggered();
      return;
    }
    parseSuccessfulResponse();
    if (this.mErrorType == RequestErrorType.NO_ERROR)
    {
      onSuccessfulResponseParsed();
      return;
    }
    onErrorTriggered();
  }

  public void parseErrorResponse()
  {
    try
    {
      JSONObject localJSONObject = new JSONObject(this.mResponseBody);
      this.mErrorCode = localJSONObject.getString("code");
      this.mErrorMessage = localJSONObject.getString("message");
      this.mErrorType = RequestErrorType.SERVER_RETURNED_ERROR;
      return;
    }
    catch (Exception localException)
    {
      SponsorPayLogger.w(getClass().getSimpleName(), "An exception was triggered while parsing error response", localException);
      this.mErrorType = RequestErrorType.ERROR_OTHER;
    }
  }

  public abstract void parseSuccessfulResponse();

  public void setResponseData(int paramInt, String paramString1, String paramString2)
  {
    this.mResponseStatusCode = paramInt;
    this.mResponseBody = paramString1;
    this.mResponseSignature = paramString2;
  }

  public boolean verifySignature(String paramString)
  {
    if (!SignatureTools.generateSignatureForString(this.mResponseBody, paramString).equals(this.mResponseSignature))
    {
      this.mErrorType = RequestErrorType.ERROR_INVALID_RESPONSE_SIGNATURE;
      return false;
    }
    return true;
  }

  public static enum RequestErrorType
  {
    static
    {
      ERROR_NO_INTERNET_CONNECTION = new RequestErrorType("ERROR_NO_INTERNET_CONNECTION", 1);
      ERROR_INVALID_RESPONSE = new RequestErrorType("ERROR_INVALID_RESPONSE", 2);
      ERROR_INVALID_RESPONSE_SIGNATURE = new RequestErrorType("ERROR_INVALID_RESPONSE_SIGNATURE", 3);
      SERVER_RETURNED_ERROR = new RequestErrorType("SERVER_RETURNED_ERROR", 4);
      ERROR_OTHER = new RequestErrorType("ERROR_OTHER", 5);
      RequestErrorType[] arrayOfRequestErrorType = new RequestErrorType[6];
      arrayOfRequestErrorType[0] = NO_ERROR;
      arrayOfRequestErrorType[1] = ERROR_NO_INTERNET_CONNECTION;
      arrayOfRequestErrorType[2] = ERROR_INVALID_RESPONSE;
      arrayOfRequestErrorType[3] = ERROR_INVALID_RESPONSE_SIGNATURE;
      arrayOfRequestErrorType[4] = SERVER_RETURNED_ERROR;
      arrayOfRequestErrorType[5] = ERROR_OTHER;
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.sponsorpay.sdk.android.publisher.AbstractResponse
 * JD-Core Version:    0.6.2
 */